<?php

class LightJob_Daemon_MockDaemon implements LightJob_IDaemon
{
	/**
	 * The lightJob
	 *
	 * @var LightJob_ILightJob
	 */
	private $lightJob;
	
	/**
	 * Stop flag
	 *
	 * @var boolean
	 */
	private $stop;
	
	public function __construct()
	{
		$this->lightJob = null;
		$this->stop = false;
		$this->length=0;
		$this->started = 0;
	}
		
	/**
	 * Sets the LightJob
	 *
	 * @param LightJob_ILightJob $lightJob
	 */
	public function setLightJob(LightJob_ILightJob $lightJob)
	{
		$this->lightJob = $lightJob;
	}
	
	/**
	 * Returns the LightJob
	 *
	 * @return LightJob_ILightJob
	 */
	public function getLightJob()
	{
		return $this->lightJob;
	}
	
	/**
	 * Starts the daemon
	 *
	 */
	public function run()
	{
		if($this->lightJob===null)
		{
			throw new NullException("No light job specified");
		}
		$this->started = time();
		while($this->stop===false)
		{
			try
			{
				$this->lightJob->process();
			}
			catch(EndException $e)
			{
				$this->markStop();
			}
			$this->checkTime();
		}
		$this->started = 0;
	}
	
	public function setRunningLength($length)
	{
		$this->length = $length;
	}
	
	protected function checkTime()
	{
		if((time()-$this->started)>$this->length){
			$this->markStop();
		}
	}
	public function markStop()
	{
		$this->stop = true;
	}
}